package com.itheima.controller.v1;

import com.itheima.common.R;
import com.itheima.controller.v1.api.ContractControllerApi;
import com.itheima.dto.ContractQueryDto;
import com.itheima.pojo.TbContract;
import com.itheima.service.TbContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/contract")
@Slf4j
@CrossOrigin
public class ContractController implements ContractControllerApi {
    @Autowired
    private TbContractService tbContractService;
    @Autowired
    private RedisTemplate redisTemplate;

    //合同分页查询列表展示接口
    @Override
    @GetMapping("/list")
    public R listContract(ContractQueryDto contractQueryDto) {
        R r = tbContractService.listContract(contractQueryDto);
        return r;
    }

    //新增合同接口
    @Override
    @PostMapping
    public R addContract(@RequestBody TbContract tbContract) {
        String username = (String) redisTemplate.opsForValue().get("username");
        R r = tbContractService.add(tbContract,username);
        return r;
    }

    //审核后查看合同详情,审核前查看审核流程接口
    @Override
    @GetMapping("/{id}")
    public R detailsContract(@PathVariable String id) {
        R r = tbContractService.detailsContract(id);
        return r;
    }


    //合同审核--审核通过或驳回接口
    @Override
    @PutMapping("/{id}")
    public R auditContract(@PathVariable String id, String status) {
        R r = tbContractService.auditContract(id,status);
        return r;
    }

}
